<?php
/**
 * Created By base
 * Author: Virus
 * Date: 2020/3/11
 * Time: 13:43
 */

namespace app\admin\controller;


use app\admin\logic\MenuLogic;
use app\common\lib\Arr;
use app\common\lib\Tree;
use think\App;

class Menu extends Base
{
    protected $logic = null;

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->logic = new MenuLogic();
    }

    public function index()
    {
        $where            = [];
        $where['field']   = [
            'id',
            'name',
            'title',
            'type',
            'status',
            'condition',
            'is_menu',
            'icon',
            'create_time',
            'pid',
        ];
        $where['status']  = 1;
        $where['is_menu'] = 1;
        $where['order']   = "id asc";
        $lists            = $this->logic->getLists($where);
        $tree             = Tree::instance();
        $lists            = $tree->init($lists, 'pid');
        $lists            = $tree->getTreeList($tree->getTreeArray('0'), 'title');

        return view('menu/index', compact('lists'));
    }

    private function getAllPermissions()
    {
        $where           = [];
        $where['field']  = [
            'id',
            'name',
            'title',
            'pid',
        ];
        $where['status'] = 1;
        $permissions     = $this->logic->getLists($where);

        return $permissions;
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            try {
                $res = $this->logic->saveData($data);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            if ($res) {
                $this->success($data['title']."权限添加成功", url("Menu/index"));
            }
            $this->error("添加失败");
        } else {
            $pid = input("param.pid", 0, "intval");
            // 获取所有权限，并分层
            $permissions = $this->getAllPermissions();
            $tree        = Tree::instance();
            $permissions = $tree->init($permissions, 'pid');
            $permissions = $tree->getTreeList($tree->getTreeArray('0'), 'title');

            return view('menu/add', compact('permissions', 'pid'));
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $id   = $this->request->post('id', 0, 'intval');
            $data = [
                'is_menu'   => $this->request->post('is_menu', 0, 'intval'),
                'pid'       => $this->request->post('pid', 0, 'intval'),
                'title'     => $this->request->post('title', '', 'trim'),
                'name'      => $this->request->post('name', '', 'trim'),
                'icon'      => $this->request->post('icon', '', 'trim'),
                'condition' => $this->request->post('condition', '', 'trim'),
            ];
            try {
                $res = $this->logic->updateDataById($id, $data);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            if ($res) {
                $this->success($data['title']."权限编辑成功", url("Menu/index"));
            }
            $this->error("编辑失败");
        } else {
            $id       = $this->request->param('id');
            $menu_one = $this->logic->findOne($id);
            if (empty($menu_one)) {
                $this->error('该权限不存在');
            }
            // 获取所有权限，并分层
            $permissions = $this->getAllPermissions();
            $tree        = Tree::instance();
            $permissions = $tree->init($permissions, 'pid');
            $permissions = $tree->getTreeList($tree->getTreeArray('0'), 'title');

            return view('menu/edit', compact('menu_one', 'permissions'));
        }
    }

    /**
     * 删除菜单
     */
    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        try {
            $res = $this->logic->deleteById($id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        if ($res) {
            $this->success("删除成功", url('Menu/index'));
        }
        $this->error('删除失败');
    }
}